<?php

namespace app\admin\logic;

use support\Backend\BackendLogic;
use support\Container;
use support\Exception\ApiException;
use support\Service\LogService;

class LogLogic extends BackendLogic
{


    public function __construct()
    {
        $this->service = Container::get(LogService::class);
    }
    /**
     * 登录日志列表
     * @return array
     */
    public  function loginLogList() :array
    {
        $param = request()->get();
        $where[] = ['type', "<", 3];
        $where[] = ['mark', "=", 1];
        if (!empty($param['username'])) {
            $where[] = ["username", 'like', "%{$param['username']}%"];
        }
        if (!empty($param['ip'])) {
            $where[] = ["ip", '=', $param['ip']];
        }
        if (!empty($param['time'])) {
            $where[] = ["create_time", '>=', $param['time'][0]];
            $where[] = ["create_time", '<', $param['time'][1]];
        }
        $list =  $this->service->getListPage($where, ['*'], ['id' => 'desc'], [], request()->get('limit', 10));
        if ($list['total'] <= 0) return message($list);
        foreach ($list['list'] as $k => $v) {
            $list['list'][$k]['ip_city'] =ip_to_address($v['ip']);
        }
        return message($list);

    }

    /**
     * 操作日志列表
     * @return array
     */
    public  function actionLogList() :array
    {
        $param = request()->get();
        $where[] = ['mark', "=", 1];
        $where[] = ['type', "=", 3];
        if (!empty($param['username'])) {
            $where[] = ["username", 'like', "%{$param['username']}%"];
        }
        if (!empty($param['ip'])) {
            $where[] = ["ip", '=', $param['ip']];
        }
        if (!empty($param['time'])) {
            $where[] = ["create_time", '>=', $param['time'][0]];
            $where[] = ["create_time", '<', $param['time'][1]];
        }
        $list =  $this->service->getListPage($where, ['*'], ['id' => 'desc'], [], request()->get('limit', 10));
        if ($list['total'] <= 0) return message($list);
        foreach ($list['list'] as $k => $v) {
            $list['list'][$k]['ip_city'] =ip_to_address($v['ip']);
        }
        return message($list);
    }

}
